package com.wokeMy.view.channe.gaohuitong;

import android.app.Dialog;
import android.content.Intent;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.woke.MainActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.model.Constant;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;

public class GhtZfActivity extends BaseActivity implements View.OnClickListener{

    Data_bindcarlist listdata,paylistdata;
    Intent intent;
    float money;
    TextView fk_carno_double,fk_shoucarno_double,fk_carname_double,fk_idcarno_double,money_double,cvv_double,yxq_double,phone_double,hqyzm_double_tv;
    EditText yzm_double_et;
    String user_id,kspayorderid,yzm,orderno;
    int way_id;
    LinearLayout yxq_double_ll,cvv_double_ll;
    View line_double_view,linetwo_double_view;
    ImageView back_double_iv;
    Button czhi_double_bt;
    Dialog dialog;
    RelativeLayout yzm_double_rl;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_html_double);
    }

    @Override
    public void initView() {
        fk_shoucarno_double=myFindViewById(R.id.fk_shoucarno_double);
        fk_carno_double=myFindViewById(R.id.fk_carno_double);
        fk_carname_double=myFindViewById(R.id.fk_carname_double);
        fk_idcarno_double=myFindViewById(R.id.fk_idcarno_double);
        money_double=myFindViewById(R.id.money_double);
        cvv_double=myFindViewById(R.id.cvv_double);
        yxq_double=myFindViewById(R.id.yxq_double);
        phone_double=myFindViewById(R.id.phone_double);
        yzm_double_et=myFindViewById(R.id.yzm_double_et);
        hqyzm_double_tv=myFindViewById(R.id.hqyzm_double_tv);
        yxq_double_ll = myFindViewById(R.id.yxq_double_ll);
        cvv_double_ll = myFindViewById(R.id.cvv_double_ll);
        line_double_view = myFindViewById(R.id.line_double_view);
        linetwo_double_view = myFindViewById(R.id.linetwo_double_view);
        back_double_iv = myFindViewById(R.id.back_double_iv);
        czhi_double_bt = myFindViewById(R.id.czhi_double_bt);
        yzm_double_rl = myFindViewById(R.id.yzm_double_rl);
        yzm_double_rl.setVisibility(View.GONE);
        hqyzm_double_tv.setOnClickListener(this);
        back_double_iv.setOnClickListener(this);
        czhi_double_bt.setOnClickListener(this);
    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Datas_load datas_load=myApp.getDatas_load();
        user_id=datas_load.getId();
        intent=getIntent();
        paylistdata=(Data_bindcarlist)intent.getSerializableExtra("paylistdata");
        listdata=(Data_bindcarlist)intent.getSerializableExtra("listdata");
        money = intent.getFloatExtra ("money", 0.01f);
        way_id = intent.getIntExtra ("way_id", 0);
        orderno = intent.getStringExtra("orderno");
        Log.e("initData2",money+"////"+way_id);
    }

    @Override
    public void initActivity() {
        String shou_card_no =listdata.cardno;
        fk_shoucarno_double.setText(shou_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+shou_card_no.substring(shou_card_no.length()-3,shou_card_no.length()));

        if (paylistdata.type.equals("0")){
            //信用卡
            yxq_double_ll.setVisibility(View.VISIBLE);
            cvv_double_ll.setVisibility(View.VISIBLE);
            line_double_view.setVisibility(View.VISIBLE);
            linetwo_double_view.setVisibility(View.VISIBLE);
            cvv_double.setText(paylistdata.code);
            yxq_double.setText(paylistdata.validthru);
        }
        String pay_card_no =paylistdata.cardno;
        fk_carno_double.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        fk_carname_double.setText(paylistdata.name);
        pay_card_no=paylistdata.idcard;
        fk_idcarno_double.setText(pay_card_no.substring(0, 4)+"    "+"***"+"    "+"***"+"    "+"***"+"    "+pay_card_no.substring(pay_card_no.length()-3,pay_card_no.length()));
        money_double.setText(money+"");
        phone_double.setText(paylistdata.phone);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hqyzm_double_tv:
                if (!yzmfastClick()){
                    hqYzm();
                    /** 倒计时60秒，一次1秒 */
                    CountDownTimer timer = new CountDownTimer(60*1000, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            // TODO Auto-generated method stub
                            hqyzm_double_tv.setText(" "+millisUntilFinished/1000+"秒"+" ");
                        }

                        @Override
                        public void onFinish() {
                            hqyzm_double_tv.setText("获取验证码");
                        }
                    }.start();
                }
                break;
            case R.id.back_double_iv:
                finish();
                break;
            case R.id.czhi_double_bt:
                 if (!fastClick()){
                        dialog= createLoadDial();
                        zf();
                 }
                break;
            default:
                break;
        }
    }
    // 获取高汇通验证码
    public void  hqYzm(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("orderNo",orderno);
        Log.e("获取高汇通验证码params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.GHTYZM_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("获取高汇通验证码result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.getString("flag").equals("success")){
                            JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                            if (jsonObject1.getString("status").equals("00")){
                                kspayorderid=jsonObject1.getString("kspayorderid");
                            }else {
                                tishi(jsonObject1.getString("refmsg"));
                            }
                        }else {
                            tishi(jsonObject.getString("info"));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

            }
        });
    }
    // 高汇通确认支付
    public void  zf(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("card_no",paylistdata.cardno); //付款卡
        params.put("orderNo",orderno);
        params.put("smsCode",""); // 0 无验证码
        Log.e("高汇通确认支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.GHTZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                closeLoadDial(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("高汇通确认支付result", result);
                    try {
                        JSONObject jsonObject =new JSONObject(result);
                        if (jsonObject.get("flag").equals("success")) {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                                String dealcode=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealcode");
                                if (dealcode.equals("10000")){
                                    Intent intent = new Intent(GhtZfActivity.this,
                                            MainActivity.class);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                    startActivity(intent);
                                    finish();
                                    tishiToast("支付成功");
                                }
                            }
                            tishi(info);
                        } else {
                            String info = jsonObject.getString("info");
                            if (jsonObject.has("data")){
                                info=jsonObject.getJSONObject("data").getJSONObject("data").getString("dealmsg");
                            }
                            tishi(info);
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                closeLoadDial(dialog);
            }
        });
    }

}
